#!/usr/bin/perl -w
use strict;
use LWP::Simple;
use XML::LibXML;
use Encode;
use URI::Escape;

use constant FORECASTMAP => 'http://weather.livedoor.com/forecast/rss/forecastmap.xml';

my $res = get(FORECASTMAP)
    or die "Failed to fetch: " . FORECASTMAP;

my @cities;
my $parser = XML::LibXML->new;
my $xml = $parser->parse_string($res);
for my $node ($xml->findnodes('//city/@title')) {
    push @cities, encode('utf8', $node->getValue);
}

my $i = 1;
for my $city (@cities) {
    my ($lon, $lat);
    $res = get('http://maps.google.co.jp/maps?q=' . uri_escape($city) .
	       '&output=kml');
    if ($res && $res =~ /coordinates>([0-9.]+),([0-9.]+),/is) {
	($lon, $lat) = ($1, $2);
    }
    print "[$i,'$city',$lat,$lon]\n";
    $i++;
}
